﻿// ESRI.ADF.UI.ZoomToScale.js
/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>

Type.registerNamespace('ESRI.ADF.UI');

ESRI.ADF.UI.ZoomToScale = function(element) {
    /// <summary>
    /// ZoomToScale is a control for zooming to a particular scale on the map.
    /// </summary>
    ESRI.ADF.UI.ZoomToScale.initializeBase(this, [element]);
    this._dpi = 96;
    this._upp = 1.0;
    this._mapLevels = 1;
    this._zoomToScaleLastScale = 0;
    this._zoomToScalePanel = null;
    this._dropdown = null;
    this._textbox = null;
    this._zoombutton = null;
}
ESRI.ADF.UI.ZoomToScale.prototype = {
    initialize: function () {
        /// <summary>
        /// Initialize the parent along with any child controls that have not yet been initialized.
        /// </summary>
        ESRI.ADF.UI.ZoomToScale.callBaseMethod(this, 'initialize');
        var id = this.get_id();
        this._zoomToScalePanel = $get(this.get_id());
        if (screen.deviceXDPI)
            this._dpi = screen.deviceXDPI;

        if (this._zoomToScalePanel != null) {

            this._dropdown = $get(id + "_Scale");
            this._zoombutton = $get(id + "_ZoomToScale");
            this._textbox = $get(id + "_ScaleText");
            alert(this._value);
            $addHandler(this._dropdown, "change", Function.createDelegate(this, this._zoomToScaleSelectChange));
            $addHandler(this._zoombutton, "mousedown", Function.createDelegate(this, this._zoomToScaleTextExecute));
            $addHandler(this._textbox, "keydown", Function.createDelegate(this, this._keyPress));
            $addHandler(this._zoombutton, "keydown", Function.createDelegate(this, this._keyPress));

            this._updateZoomToScaleElements();
        }
        if (this._map != null) {
            this._map.add_extentChanged(Function.createDelegate(this, this._updateZoomToScaleElements));
            this._map.get_layers().add_layerAdded(Function.createDelegate(this, this._updateZoomToScaleElements));
            this._map.get_layers().add_layerRemoved(Function.createDelegate(this, this._updateZoomToScaleElements));
        }
    },
    dispose: function () {
        ESRI.ADF.UI.ZoomToScale.callBaseMethod(this, 'dispose');
    },
    get_map: function () {
        /// <value name="map" type="Object">Map control associated with the GoToLocation functionality</value>
        return this._map;
    },
    set_map: function (value) {
        this._map = value;
    },
    get_conversionFactor: function () {
        /// <value name="conversionFactor" type="String">Value of scale conversion factor</value>
        return this._conversionFactor;
    },
    set_conversionFactor: function (value) {
        alert(value);
        this._conversionFactor = value;
    },

    _keyPress: function (e) {
        if (e && e.keyCode == Sys.UI.Key.enter) {
            this._zoomToScaleTextExecute();
        }
    },
    _updateZoomToScaleElements: function () {
        /// <summary>
        /// Updates the display
        /// </summary>


        var html = '';
        var m = this.get_map();
        if (m.get_layers().get_levels() !== null) this._mapLevels = m.get_layers().get_levels().length;
        if (this._mapLevels > 1) {
            $get("UnCached").style.display = 'none';
            $get("Cached").style.display = '';
        }
        else {
            $get("Cached").style.display = 'none';
            $get("UnCached").style.display = '';
        }

        if (this._conversionFactor != 0) {
            var mapId = m.get_id();
            var mapwidth = parseInt(m.get_element().clientWidth);
            var unitsperpixel = m.get_extent().get_width() / mapwidth;
            var scale = Math.round(this._getMapScale(unitsperpixel));
            if (this._zoomToScaleLastScale != scale) {
                if (this._textbox != null)
                    this._textbox.value = "1:" + scale.localeFormat("N0");
                if (this._mapLevels > 1)
                    this._populateZoomToScaleDropdown();

                this._zoomToScaleLastScale = scale;
            }
        }
    },

    _getMapScale: function (unitsperpixel) {
        /// <summary>
        /// Gets current map scale
        /// </summary>
        /// <param name="unitsperpixel" type="Number">Number of units per pixel</param>
        var unitsperinch = unitsperpixel * this._dpi;
        return Math.round(Number.parseLocale(this._conversionFactor) * unitsperinch);
    },

    _zoomToScaleSelectChange: function () {
        /// <summary>
        /// Handler for dropdown selection - used if cached resource
        /// </summary>

        if (this._conversionFactor != 0) {
            var m = this.get_map();
            var level = this._dropdown.selectedIndex;
            var mapwidth = parseInt(m.get_element().clientWidth);
            var unitsperpixel = m.get_extent().get_width() / mapwidth;
            var levels = m.get_layers().get_levels();
            var factor = unitsperpixel / levels[level];
            m.zoom(factor);
        }
        else {
            alert('Map has unknown map units');
        }

    },

    _zoomToScaleTextExecute: function () {
        /// <summary>
        /// Handler for Zoom button clicked - used if dynamic resource
        /// </summary>
        if (this._conversionFactor != 0) {
            if (this._textbox != null) {
                var scale = this._textbox.value

                if (scale.indexOf("1:") != -1) {
                    scale = scale.substring(2);
                }
                scale = Number.parseLocale(scale);
                if (isNaN(scale) || scale <= 0)
                    alert("Nhập giá trị số lớn hơn 0.");
                else {
                    pixelsize = this._getPixelSize(scale);
                    var m = this.get_map();
                    var mapwidth = parseInt(m.get_element().clientWidth);
                    var unitsperpixel = m.get_extent().get_width() / mapwidth;
                    var factor = unitsperpixel / pixelsize;
                    m.zoom(factor);
                }
            }
        }
        else {
            alert('Map has unknown map units');
        }
    },

    _populateZoomToScaleDropdown: function () {
        /// <summary>
        /// Updates dropdown list
        /// </summary>
        var m = this.get_map();
        var id = this.get_id();
        var mapwidth = parseInt(m.get_element().clientWidth);
        var unitsperpixel = m.get_extent().get_width() / mapwidth;
        var layers = m.get_layers();
        var scale;
        if (this._dropdown != null) {
            this._dropdown.options.length = 0;
            for (var dd = 0; dd < this._scales.length; dd++) {
                this._dropdown.options[dd] = new Option(this._scales[dd], dd, false);
            }
            var level = layers.getLevelByNearestPixelsize(unitsperpixel);
            this._dropdown.selectedIndex = level;
        }
    },

    _getPixelSize: function (scale) {
        /// <summary>
        /// Get units per pixel based on scale
        /// </summary>
        /// <param name="scale" type="Number">Scale to be used</param>

        var inchesperpixel = 1 / this._dpi;
        var unitsperpixel = inchesperpixel * scale;
        unitsperpixel = unitsperpixel / Number.parseLocale(this._conversionFactor);

        return unitsperpixel;
    }
}

ESRI.ADF.UI.ZoomToScale.registerClass('ESRI.ADF.UI.ZoomToScale', Sys.UI.Control);

if (typeof (Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }

